<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * getApiSales controller
 * @author Phuc Nguyen - 09/03/2015
 * http://www.facebook.com/phucthuong
 * email : nguyenvanphuc0626@gmail.com
*/
class GetApiSales extends BACKEND_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->language('getApiSales');
		$this->load->language('button');
		if($this->database_connect_status){
			$this->load->model('getApiSales_model');
			$this->set_controller('getApiSales');
			$this->set_model($this->getApiSales_model);
		}
                
                $this->load->library('bookinglib');
                $this->booking = new bookinglib();
	}
        
        private function menuTop(){
            $this->load->model('stats_model');
            $user_info                          = $this->stats_model->get_user_info($this->stats_model->get_user_id());
            $this->view_data['role_by_group']   = $user_info['role_list'];
            $this->view_data['username']        = $user_info['username'];
            return $this->view_data;
        }
        
        public function index(){
            $this->menuTop();
            if($this->input->server('REQUEST_METHOD')=='POST'){
                
                $frm_api                    =   new stdClass();
                $frm_api->user_id           =   $this->input->post('username');
                $frm_api->permission        =   json_encode(explode(",",strtoupper($this->input->post('airline'))));
                $frm_api->owner             =   $this->session->userdata['user_id'];
                $frm_api->lastupdated       =   time();
                
                $exists = $this->getApiSales_model->find_by(array('user_id'=>$this->input->post('username')));
                
                if($exists){
                    $id = $this->getApiSales_model->update($frm_api,$exists[0]->id);
                } else {
                    $id = $this->getApiSales_model->create($frm_api);
                }
                
                if($id){
                    $this->session->set_userdata('api_update',$this->lang->line('update_successful'));
                    $paramAdminLog  = array(
                        'userid'            => $this->session->userdata['user_id'],
                        'lastLogin'         => date('Y-m-d :H:i:s',time()),
                        'ip'                => $_SERVER['REMOTE_ADDR'],
                        'logAction'         => 'Phan quyen API Sale thành công >> Nhân viên id = '.$this->input->post('username'),
                        'agent_code'        => $this->session->userdata['agent_code']
                    );
                    $this->user_model->insertUserAdminLog($paramAdminLog);

                    redirect('auth/getApiSales');
                }
            }
            
            $saler      = $this->user_model->find_by(array('group_id'=>74,'active'=>1));
            $booker     = $this->user_model->find_by(array('group_id'=>78,'active'=>1));
            $user_info[] = '--Choosen--';
            foreach(array_merge($saler,$booker) as $ks => $vs){
                $user_info[$vs->id]             =   $vs->firstname;
            }
            
            if(isset($this->session->userdata['api_update']) && $this->session->userdata['api_update']){
                $this->view_data['api_update']          =   $this->session->userdata['api_update'];
                $this->session->unset_userdata('api_update');
            }
            
            $this->view_data['data_user']               =   $user_info;
            $this->view_data['lists']                   =   $this->getApiSales_model->find_by();
            $this->load->view('auth/getApiSales/index', $this->view_data);
        }
        
        public function getInfo(){
            if($this->input->server('REQUEST_METHOD')=='POST'){
                $data = $this->getApiSales_model->find_by(array('user_id'=>$this->input->post('user_id')));
                if(empty($data)){
                    $data = "";
                } else {
                    $data = $data[0]->permission;
                }
            }
            die($data);
        }
}